package com.dnjn.mes.biz.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dnjn.mes.biz.domain.MesExcept;
import com.dnjn.mes.biz.domain.MesResolvePlan;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 异常编码表 Mapper 接口
 * </p>
 *
 * @author Amber
 * @since 2023-07-26
 */
public interface MesExceptMapper extends BaseMapper<MesExcept> {

    List<MesExcept> selectExceptList(MesExcept domain);

    //@Select("select a.*,c.plan_id,c.description from mes_except a left join mes_except_plan_xref b on a.except_id=b.except_id LEFT JOIN mes_resolve_plan c on c.plan_id=b.plan_id where a.assembly_id = #{assemblyId} and a.deleted = '0' ")
    @Select("select a.* from mes_except a where a.assembly_id = #{assemblyId} and a.deleted = '0' ")
    List<MesExcept> getExceptPageByAssemblyId(Long assemblyId);

    @Update("update mes_except set deleted = '1' where except_id = #{exceptId}")
    int removeExceptById(@Param("exceptId") Long exceptId);

    @Select("select c.description from mes_except a left join mes_except_plan_xref b on a.except_id = b.except_id left join mes_resolve_plan c on b.plan_id = c.plan_id  where a.except_id = #{exceptId} and a.deleted = '0' and c.deleted = '0' LIMIT 1 ")
    String getDescriptionByExceptId(@Param("exceptId") Long exceptId);

    @Select("SELECT a.except_id,b.plan_id,b.description,c.workpiece_name FROM mes_except_plan_xref a LEFT JOIN mes_resolve_plan b ON a.plan_id=b.plan_id LEFT JOIN mes_workpiece c ON b.workpiece_id=c.workpiece_id WHERE a.except_id=#{exceptId} AND b.deleted='0'AND c.deleted='0' AND b.equipment_id=#{equipmentId} AND c.equipment_id=#{equipmentId} ORDER BY b.ordinal ")
    List<MesResolvePlan> getPlansByExceptId(@Param("exceptId")Long exceptId, @Param("equipmentId")long equipmentId);
}
